layui.use(['laydate','form','table','laytpl','layer'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;
	var laytpl = layui.laytpl;
	var layer = layui.layer;
	/*@时间*/
	laydate.render({
	  	elem: '#start' //指定元素
	});
	laydate.render({
	  	elem: '#end' //指定元素
	});
	/*@初始化table*/
	tableObj = table.render({
	    elem: '#table-list',
	    id: 'list',
	    url: '/finance/income/index', //数据接口
	    method: 'post',
	    page: true, //开启分页
	    cols: [[ //表头
	    	// {field: 'expend_id', title: '序号'},
	    	{field: 'addtime', title: '下单时间'},
	    	{field: 'or_sn', title: '订单编号/维修编号'},
	    	{field: 'shop_name', title: '所属门店'},
	    	{field: 'username', title: '客户姓名'},
	    	{field: 'phone', title: '联系方式'},
	    	{field: 'user_sn', title: '客户编号'},
	    	{field: 'order_ty', title: '订单类型'},
	    	{field: 'total_money', title: '订单金额(元)'},
	    	{field: 'cost_money', title: '订单成本(元)'},
	    	{field: 'profit_money', title: '订单收入(元)'},
	    	{field: 'state_text', title: '订单状态(元)'},
	    	{field: 'stay_money', title: '待支付金额(元)'},
	    	{title: '操作', toolbar: '#table-btn'}
	    ]],
	    parseData: function(res){
	    	let coun = res.data.count
	    	res.data = res.data.list
	    	let getTpl_2 = count_box.innerHTML;
			let count = document.getElementById('count-box');
			laytpl(getTpl_2).render(coun, function(html){
				count.innerHTML = html;
			});
	    }
	});

	// /*@查询*/
	form.on('submit(search)',function(data){
		console.log(data)
		/*重置表格数据*/
		tableObj.reload({
			page: {
                page: 1 //重新从第 1 页开始
            },
            where: data.field
        });
        return false
	})
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'del'){ //删除
	  		let obj_data = {}
	  		obj_data.id = data.expend_id
	  		del(obj_data)
	  	}
	});
	/*@删除*/
	function del(obj_data){
		$.post('/finance/pay/del', obj_data, function(data){
			if(data.code !== 1){
				layer.msg(data.msg, {icon: 5});
			}else{
				layer.msg(data.msg, {icon: 6, time: 1000}, function(){
					table.reload('list')
				})
			}
		})
	}
});